package cc.zy.base.businesses.service.impl;

import cc.zy.base.businesses.entity.Notice;
import cc.zy.base.businesses.mapper.NoticAddMapper;
import cc.zy.base.businesses.service.INoticAddService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhaojiewei
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Slf4j
public class NoticAddServiceImpl implements INoticAddService {

    private final NoticAddMapper noticeAddMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertNotice(Notice notice, String userIdss) {

        log.debug(userIdss);
        String[] str = userIdss.split("},");

        for (int i = 0; i < str.length - 1; i++) {
            str[i] = str[i] + "}";
        }

        List<String> stuIds = new ArrayList<>();
        List<String> stuPoolIds = new ArrayList<>();

        for (String s : str) {
            HashMap userid = JSON.parseObject(s, HashMap.class);
            Set<Map.Entry<String, String>> entries = userid.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if (entry.getKey().equals("2")){
                    String[] ss = entry.getValue().split(",");
                    for (String s1 : ss) {
                        stuIds.add(s1);
                    }
                }else if (entry.getKey().equals("1")){
                    String[] sss = entry.getValue().split(",");
                    for (String s2 : sss) {
                        stuPoolIds.add(s2);
                    }
                }
            }
        }

        log.debug(String.format("stuIds = %s", stuIds));
        log.debug(String.format("stuPoolIds = %s", stuPoolIds));
        HashSet<String> set = new HashSet<>(stuIds) ;
        HashSet<String> setPool = new HashSet<>(stuPoolIds);

        noticeAddMapper.insertNotice(notice);

        if(stuIds.size()>0){
            noticeAddMapper.insertNoticeUser(notice, set);
        }
        if (stuPoolIds.size()>0){
            noticeAddMapper.insertNoticeUserPool(notice, setPool);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertNoticeTemp(Notice notice, String userIdss) {

        log.debug(userIdss);
        String[] str = userIdss.split("},");

        for (int i = 0; i < str.length - 1; i++) {
            str[i] = str[i] + "}";
        }

        List<String> stuIds = new ArrayList<>();
        List<String> stuPoolIds = new ArrayList<>();

        for (String s : str) {
            HashMap userid = JSON.parseObject(s, HashMap.class);
            Set<Map.Entry<String, String>> entries = userid.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if (entry.getKey().equals("2")){
                    String[] ss = entry.getValue().split(",");
                    for (String s1 : ss) {
                        stuIds.add(s1);
                    }
                }else if (entry.getKey().equals("1")){
                    String[] sss = entry.getValue().split(",");
                    for (String s2 : sss) {
                        stuPoolIds.add(s2);
                    }
                }
            }
        }

        log.debug(String.format("stuIds = %s", stuIds));
        log.debug(String.format("stuPoolIds = %s", stuPoolIds));

        HashSet<String> set = new HashSet<>(stuIds) ;
        HashSet<String> setPool = new HashSet<>(stuPoolIds);

        noticeAddMapper.insertNoticeTemp(notice);

        if(stuIds.size()>0){
            noticeAddMapper.insertNoticeUser(notice, set);
        }

        if(stuPoolIds.size()>0){
            noticeAddMapper.insertNoticeUserPool(notice, setPool);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendNotice(Integer id) {
        noticeAddMapper.sendNotice(id);
    }
}
